package org.shiki.hrm.interceptor;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;

@Component
public class UserInfoFeignInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate requestTemplate) {
		// 使用客户端模式,生成token
		String url = "http://localhost:3010/oauth/token?client_id=admin&client_secret=1&grant_type=client_credentials&redirect_uri=http://www.baidu.com&username=lyp&password=1";
		String result = HttpUtil.get(url);
		// 将请求结果转换为json对象
		Map<String, Object> map = JSON.parseObject(result, Map.class);
		// 获取token
		String token = map.get("access_token").toString();
		System.err.println(token);
		// 设置到请求头
		requestTemplate.header("Authorization", "bearer " + token);
	}
}
